package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.project.common.aop.annotation.DataScope;
import com.company.project.common.utils.DataResult;
import com.company.project.entity.SysClassificationEntity;
import com.company.project.entity.SysUser;
import com.company.project.entity.TProjectInfoEntity;
import com.company.project.service.SysClassificationService;
import com.company.project.service.TProjectInfoService;
import com.company.project.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


/**
 * 项目信息表
 *
 * @author iukcy
 * @email iukcy2018@163.com
 * @date 2022-03-01 16:57:35
 */
@Controller
@RequestMapping("/")
public class TProjectInfoController {
    @Autowired
    private UserService userService;
    @Autowired
    private TProjectInfoService tProjectInfoService;
    @Autowired
    private SysClassificationService classificationService;

    /**
     * 跳转到页面
     */
    @GetMapping("/index/project")
    public String tProjectInfo() {
        return "projectinfo/list";
    }

    @ApiOperation(value = "新增")
    @PostMapping("project/add")
    @RequiresPermissions("tProjectInfo:add")
    @ResponseBody
    public DataResult add(@RequestBody TProjectInfoEntity tProjectInfo) {
        //提交时间
        tProjectInfo.setPCreateTime(new Date());
        //待启动状态
        tProjectInfo.setPStatus(0);
        if (!StringUtils.isEmpty(tProjectInfo.getPDirectorId())) {
            SysUser userInfo = userService.getById(tProjectInfo.getPDirectorId());
            if (userInfo != null) {
                tProjectInfo.setPDirector(userInfo.getRealName());
            }
        }
        tProjectInfoService.save(tProjectInfo);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("project/delete")
    @RequiresPermissions("tProjectInfo:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
        tProjectInfoService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("project/update")
    @RequiresPermissions("tProjectInfo:update")
    @ResponseBody
    public DataResult update(@RequestBody TProjectInfoEntity tProjectInfo) {
        tProjectInfoService.updateById(tProjectInfo);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("project/listByPage")
    @RequiresPermissions("tProjectInfo:list")
    @ResponseBody
    @DataScope
    public DataResult findListByPage(@RequestBody TProjectInfoEntity tProjectInfo) {
        Page page = new Page(tProjectInfo.getPage(), tProjectInfo.getLimit());
        LambdaQueryWrapper<TProjectInfoEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        if (!StringUtils.isEmpty(tProjectInfo.getPName())) {
            queryWrapper.like(TProjectInfoEntity::getPName, tProjectInfo.getPName());
        }
        if (!StringUtils.isEmpty(tProjectInfo.getPYear())) {
            queryWrapper.eq(TProjectInfoEntity::getPYear, tProjectInfo.getPYear());
        }
        if (!StringUtils.isEmpty(tProjectInfo.getPDirector())) {
            queryWrapper.like(TProjectInfoEntity::getPDirector, tProjectInfo.getPDirector());
        }
        if (!StringUtils.isEmpty(tProjectInfo.getPClassId()) && !"0".equals(tProjectInfo.getPClassId())) {
            SysClassificationEntity sysClass = classificationService.getById(tProjectInfo.getPClassId());
            if ("0".equals(sysClass.getPId())) {
                LambdaQueryWrapper<SysClassificationEntity> sysClassQueryWrapper = Wrappers.lambdaQuery();
                sysClassQueryWrapper.select(SysClassificationEntity::getId).eq(SysClassificationEntity::getPId, sysClass.getId());
                List<Object> list = classificationService.selectObjs(sysClassQueryWrapper);
                queryWrapper.in(TProjectInfoEntity::getPClassId, list);
            } else {
                queryWrapper.eq(TProjectInfoEntity::getPClassId, tProjectInfo.getPClassId());
            }
        }
        //数据权限示例， 需手动添加此条件 begin
        if (!CollectionUtils.isEmpty(tProjectInfo.getCreateIds())) {
            queryWrapper.in(TProjectInfoEntity::getPDirectorId, tProjectInfo.getCreateIds());
        }
        queryWrapper.orderByAsc(TProjectInfoEntity::getPYear,TProjectInfoEntity::getPFinishTime);
        IPage<TProjectInfoEntity> iPage = tProjectInfoService.page(page, queryWrapper);
        iPage.getRecords().forEach(item -> {
            SysClassificationEntity classInfo = classificationService.getById(item.getPClassId());
            if (classInfo != null) {
                item.setPClassName(classInfo.getName());
            }
        });
        return DataResult.success(iPage);
    }

}
